import tap from './tap'
import double from './double'
import hold from './hold'
import swipe from './swipe'
import drag from './drag'

const event = {
  tap,
  double,
  hold,
  swipe,
  drag,
}

const touch = {
  bind(el, binding) {
    const modifiers = binding.modifiers
    const stop = binding.modifiers.stop || false
    const prevent = binding.modifiers.prevent || false
    const callback = binding.value

    for(var i in modifiers)
      if(event[i])
        event[i].bind(el, binding)

    el.addEventListener('touchstart', function(event){
      callback(event)
      if(stop)
        event.stopPropagation()
      if(prevent)
        event.preventDefault()
    }, false)

    el.addEventListener('touchmove', function(event){
      callback(event)
      if(stop)
        event.stopPropagation()
      if(prevent)
        event.preventDefault()
    }, false)

    el.addEventListener('touchend', function(event){
      callback(event)
      if(stop)
        event.stopPropagation()
      if(prevent)
        event.preventDefault()
    }, false)

    el.addEventListener('touchenter', function(event){
      callback(event)

      if(prevent)
        event.preventDefault()
    }, false)

    el.addEventListener('touchleave', function(event){
      callback(event)

      if(prevent)
        event.preventDefault()
    }, false)

    el.addEventListener('touchcancel', function(event){
      callback(event)
      if(stop)
        event.stopPropagation()
      if(prevent)
        event.preventDefault()
    }, false)
  }
}

export default touch
export {
  tap,
  double,
  hold,
  swipe,
  drag,
}
